//
//  JCAddBankCardController.m
//  JiFengDaRen
//
//  Created by shouhan on 2018/3/29.
//  Copyright © 2018年 陈永发. All rights reserved.
//

#import "JCAddBankCardController.h"
#import "JCAddBankView.h"
#import "SHNerAreaList.h"
#import "SHCreditCardSearchController.h"
#import "SHBankInfoModel.h"

@interface JCAddBankCardController ()<SHNerAreaListDelegate,SHCreditCardSearchControllerDelegate>

@property (nonatomic, strong) JCAddBankView * addbankView;

@property (nonatomic, strong) NSString * strProvince;
@property (nonatomic, strong) NSString * strCity;
@property (nonatomic, strong) NSString * bankName;

@end

@implementation JCAddBankCardController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = KBASEBGCOLOR;
    
    self.title = @"添加付款银行卡";
    
    self.addbankView = [[JCAddBankView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - KNAVIGATIONBARHEIGHT)];
    
    [self.view addSubview:self.addbankView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addBankAction:) name:@"addCard" object:nil];
    
}


- (void)addBankAction:(NSNotification *)tag{
    
    NSLog(@"%@",tag.userInfo[@"tag"]);
    NSInteger index = [tag.userInfo[@"tag"] integerValue];
    switch (index) {
        case 1001:{
            [self popAreaList];
        }
            break;
        case 1002:{
            [self popAreaList];
        }
            break;
        case 1003:{

            [self popBranch];
        }
            break;
        case 1004:{

            [self didConfirmAction];
        }
            break;
        default:
            break;
    }
    
}
// 弹出城市选择框
- (void)popAreaList{
    [self.view endEditing:YES];
    SHNerAreaList *areaList = [[SHNerAreaList alloc]init];
    areaList.delegate = self;
    [areaList show];
}

#pragma mark - 地址选择代理
- (void)pickerArea:(SHNerAreaList *)pickerArea province:(NSString *)province city:(NSString *)city  provinceId:(NSString *)provinceId cityId:(NSString *)cityId  area:(NSString *)area{
    
    [self.addbankView getPickerAreaProvince:province City:city];
    
    self.strProvince = province;
    self.strCity = city;
}

// 跳转支行名称
- (void)popBranch{
    
    [self.view endEditing:YES];
    if (self.strProvince.length == 0) {
        [self.view makeToast:@"请选择省份" duration:1.5 position:@"center"];
        return;
    }
    if (self.strCity.length == 0) {
        [self.view makeToast:@"请选择城市" duration:1.5 position:@"center"];
        return;
    }
    SHCreditCardSearchController *cardSearchVc = [[SHCreditCardSearchController alloc]init];
    cardSearchVc.delegate = self;
    [self.navigationController pushViewController:cardSearchVc animated:YES];
}

#pragma mark - SHCreditCardSearchControllerDelegate
- (void)searchResultCardModel:(SHBankInfoModel *)cardModel{
    
    SHLog(@"搜索结果 == %@",cardModel.bankName);
    self.bankName = cardModel.bankName;
    [self.addbankView getBankName:cardModel.bankName];
}


- (void)didConfirmAction{
    [self.view endEditing:YES];
    
    if (self.addbankView.bankField.text.length == 0) {
        [self.view makeToast:@"请输入银行卡号" duration:1.5 position:@"center"];
        return;
    }else{
        
        if (![SHHelper checkCardNo:self.addbankView.bankField.text]) {
            [self.view makeToast:
             @"请输入正确的银行卡号" duration:1.f position:@"center"];
            return;
        }
    }
    
    if (self.addbankView.phoneField.text.length == 0) {
        [self.view makeToast:@"请输入预留手机号" duration:1.5 position:@"center"];
        return;
    }
    
    if (!self.strProvince) {
        [self.view makeToast:@"请选择省份" duration:1.5 position:@"center"];
        return;
    }
    
    if (!self.strCity) {
        [self.view makeToast:@"请选择城市" duration:1.5 position:@"center"];
        return;
    }
    
    if (!self.bankName) {
        [self.view makeToast:@"请选银行名称" duration:1.5 position:@"center"];
        return;
    }
    [self DoRequsetAddBank];
}

// 网络请求
- (void)DoRequsetAddBank{
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setValue:self.addbankView.bankField.text forKey:@"bank_card_code"];
    [dict setValue:self.addbankView.phoneField.text forKey:@"phone"];
    [dict setValue:self.strProvince forKey:@"province"];
    [dict setValue:self.strCity forKey:@"city"];
    [dict setValue:self.bankName forKey:@"bank_name"];
    [dict setValue:@"12" forKey:@"bank_card_type"];// 11是借记卡， 12 信用卡
    [dict setValue:@"2" forKey:@"using_choices"];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:KNEWBANKINFO parameters:dict cache:NO encrypt:YES decode:YES responseCache:^(id responseCache) {

    } success:^(id result) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];

        NSLog(@"result :%@",result);
        if ([result[@"success"] boolValue] == YES) {
            if ([result[@"data"] isEqualToString:@"ok"]) {
                [self.view makeToast:@"该卡已经添加过了" duration:1.5 position:@"center"];
            }else{
                [self.view makeToast:@"添加成功" duration:1.5 position:@"center"];
            }
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5/*延迟执行时间*/ * NSEC_PER_SEC));
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
    
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [self.view makeToast:result[@"data"] duration:1.5 position:@"center"];
        }
        

    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
    
}

// 销毁通知
- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
